package com.market.sys.api.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.market.sys.common.Result;
import com.market.sys.entity.Contract;
import com.market.sys.entity.Notice;
import com.market.sys.service.IContractService;
import com.market.sys.util.PageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/admin/contract")
public class AdminContractController {

    @Autowired
    private IContractService contractService;

    @PostMapping("/list")
    public Result<Map<String ,Object>> search(@RequestBody PageForm form){
        LambdaQueryWrapper<Contract> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Page<Contract> page = contractService.page(new Page<>(form.getPageNo(), form.getPageSize()), lambdaQueryWrapper);
        List<Contract> records = page.getRecords();
        for (Contract record : records) {
            if (StrUtil.isEmpty(record.getStartDate())){
                record.setStartDate("--");
            }
            if (StrUtil.isEmpty(record.getEndDate())){
                record.setEndDate("--");
            }
            if (StrUtil.isEmpty(record.getAName())){
                record.setAName("--");
            }
            if (StrUtil.isEmpty(record.getAPhone())){
                record.setAPhone("--");
            }
            if (StrUtil.isEmpty(record.getBName())){
                record.setBName("--");
            }
            if (StrUtil.isEmpty(record.getBPhone())){
                record.setBPhone("--");
            }
            if (StrUtil.isEmpty(record.getaSignDate())){
                record.setaSignDate("--");
            }
            if (StrUtil.isEmpty(record.getbSignDate())){
                record.setbSignDate("--");
            }
            if (record.getRentalTerm() == null){
                record.setRentalTerm(-1);
            }
        }

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",records);

        return Result.success(data);
    }

}
